package com.xinqi.service.impl;

import com.xinqi.service.HelloService;
import com.xinqi.utils.pool.MyJdkPool;
import com.xinqi.utils.pool.MyJdkPool2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

/**
 * @author xin麒
 * @date 2023/8/12 15:00
 */
@Slf4j
@Service
public class HelloServiceImpl implements HelloService {
    @Resource
    private ThreadPoolTaskExecutor xinPoolExecutor;
    @Override
    public Object processForCount(Long count) {
        xinPoolExecutor.execute(()->{
            try {
                Thread.sleep(1000 * 40);
                File file=new File("D:\\start_java\\XinQiUtilsOrDemo\\XinQiUtilsOrDemo\\ThreadPoolDemo\\ThreadPool05\\newfile.txt");
                file.createNewFile();
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
            log.debug("this is execute in pool");
        });
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        xinPoolExecutor.shutdown();
        return "nice";
    }
    @Autowired
    private MyJdkPool myJdkPool;
    @Override
    public Object myJdkPool(Long count) {
        myJdkPool.execute(()->{
            int i = 10 / 0;
            log.debug("this is execute in pool");
        });

        log.debug("this is a log1");


        myJdkPool.execute(()->{
            int i = 10 / 0;
            log.debug("this is execute in pool");
        });

        log.debug("this is a log2");



        return "nice";
    }
    @Override
    public Object testBySubmit(Long count) {
        Future<String> f = myJdkPool.submit(() -> {
            int i = 10 /0;
            log.debug("haha");
            return "ok";
        });

        try {
            System.out.println(f.get());
            log.debug("this is a log2");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        log.debug("this is a log3");
        return "nice";
    }



    @Autowired
    private MyJdkPool2 myJdkPool2;
    @Override
    public Object myJdkPool2(Long count) {
        myJdkPool2.execute(()->{
            int i = 10 / 0;
            log.debug("this is execute in pool");
        });

        log.debug("this is a log1");

        myJdkPool2.execute(()->{
            int i = 10 / 0;
            log.debug("this is execute in pool");
        });

        log.debug("this is a log2");
        return "nice";
    }
    @Override
    public Object testBySubmitByMyJdkPool2(Long count) {
        Future<String> f = myJdkPool2.submit(() -> {
            int i = 10 /0;
            log.debug("haha");
            return "ok";
        });

        try {
            System.out.println(f.get());
            log.debug("this is a log2");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        log.debug("this is a log3");
        return "nice";
    }





}
